﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;

namespace RemoteUser
{
    class TranslateXml
    {
        public static int GetPLSVer(XmlDocument xDocument)
        {
            XmlNode node;
            int ver = 0;

            try
            {
                node = xDocument.SelectSingleNode("/PLS/ver");
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                return -1;
            }

            try
            {
                ver = Int32.Parse(node.InnerText);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                return -1;
            }
            return ver;
        }

        public static int GetPLSType(XmlDocument xDocument)
        {
            XmlNode node;
            int type = 0;

            try
            {
                node = xDocument.SelectSingleNode("/PLS/type");
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                return -1;
            }

            try
            {
                type = Int32.Parse(node.InnerText);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                return -1;
            }
            return type;
        }

        public static int GetPLSRetrunCode(XmlDocument xDocument)
        {
            XmlNode node;
            int returnCode = 0;

            try
            {
                node = xDocument.SelectSingleNode("/PLS/returnCode");
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                return -1;
            }

            try
            {
                returnCode = Int32.Parse(node.InnerText);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                return -1;
            }
            return returnCode;
        }

        public static string GetPLSMsg(XmlDocument xDocument)
        {
            XmlNode node;
            string msg = default(string);

            try
            {
                node = xDocument.SelectSingleNode("/PLS/msg");
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                return null;
            }

            try
            {
                msg = node.InnerText;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                return null;
            }
            return msg;
        }
        //本函数需要改进
        public static BindingCollection<PLC> GetPLSPLCList(XmlDocument xDocument)
        {
            try 
            { 
                XmlNodeList PLCNodeList = xDocument.SelectNodes("/PLS/PLC");
                int parmNum = 0;
                var PLCs = new BindingCollection<PLC>();
                if (PLCNodeList != null)
                {
                    foreach (XmlNode PLCNodes in PLCNodeList)
                    {
                        string[] tempPLCParm = new string[7];
                        parmNum = 0;
                        foreach (XmlNode PLCNode in PLCNodes)
                        {
                            tempPLCParm[parmNum++] = PLCNode.InnerText;
                        }
                        PLCs.Add(new PLC() { PLCID = tempPLCParm[0],
                                                    PLCName = tempPLCParm[1],
                                                    PLCStatus = tempPLCParm[2],
                                                    PLCCompany = tempPLCParm[3],
                                                    PLCProject = tempPLCParm[4],
                                                    PLCAbnormal = tempPLCParm[5],
                                                    PLCMsg = tempPLCParm[6]});
                        tempPLCParm = null;
                    }
                }
                return PLCs;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return null;
            }
        }

        public static BindingCollection<ParmNode> GetParmsList(XmlDocument xDocument,int produceCraftsOrDeviceStatus )
        {
            int parmNum = 0;
            var parmsList = new BindingCollection<ParmNode>();
            string parmsPath = null;
            if (produceCraftsOrDeviceStatus == 0)
                parmsPath = "/PLS/produceCrafts/allParameter/parm";
            else if (produceCraftsOrDeviceStatus == 1)
                parmsPath = "/PLS/deviceStatus/allParameter/parm";
            else if(produceCraftsOrDeviceStatus == 2)
                parmsPath = "/PLS/produceCrafts/keyParameter/parm";
            else if(produceCraftsOrDeviceStatus == 3)
                parmsPath = "/PLS/deviceStatus/keyParameter/parm";
            else return null;
            try
            {
                XmlNodeList allParms = xDocument.SelectNodes(parmsPath);

                if (allParms != null)
                {
                    foreach (XmlNode parmNode in allParms)
                    {
                        string[] tempParms = new string[2];
                        parmNum = 0;
                        foreach (XmlNode parms in parmNode)
                        {
                            tempParms[parmNum++] = parms.InnerText;
                        }
                        parmsList.Add(new ParmNode() { parmName = tempParms[0],
                                                        parmValue = tempParms[1]});
                        tempParms = null;
                    }
                }
                return parmsList;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return null;
            }
        }

        public static string GetParmsknowledgeInfo(XmlDocument xDocument, int produceCraftsOrDeviceStatus)
        {
            string parmsPath = null;
            string knowledgeInfo;
            if (produceCraftsOrDeviceStatus == 0)
                parmsPath = "/PLS/produceCrafts/knowledgeInfo";
            else if (produceCraftsOrDeviceStatus == 1)
                parmsPath = "/PLS/deviceStatus/knowledgeInfo";
            else if (produceCraftsOrDeviceStatus == 2)
                parmsPath = "/PLS/produceCrafts/knowledgeInfo";
            else if (produceCraftsOrDeviceStatus == 3)
                parmsPath = "/PLS/deviceStatus/knowledgeInfo";
            else return null;
            try
            {
                XmlNode keyParms = xDocument.SelectSingleNode(parmsPath);
                knowledgeInfo = keyParms.InnerText;
                return knowledgeInfo;
            }
            catch 
            {
                return null;
            }
        }

        public static QQInfo GetQQInfo(XmlDocument xDocument)
        {
            XmlNode node;
            QQInfo qqInfo = new QQInfo();
            try
            {
                node = xDocument.SelectSingleNode("/PLS/yourQQ");
                qqInfo.yourQQ = node.InnerText;
                node = xDocument.SelectSingleNode("/PLS/yourQQpwd");
                qqInfo.yourQQpwd = node.InnerText;
                node = xDocument.SelectSingleNode("/PLS/peerQQ");
                qqInfo.peerQQ = node.InnerText;

                return qqInfo;
            }
            catch
            {
                return null;
            }
        }

        public static string GetPLCConnInfo(XmlDocument xDocument)
        {
            XmlNode node;
           // string PLCConnInfo = default(string);
            StringBuilder PLCConnInfo = new StringBuilder();
            try
            {
                node = xDocument.SelectSingleNode("/PLS/companyInfo/company");
                PLCConnInfo.AppendFormat("公司名称：{0} \n", node.InnerText);
                node = xDocument.SelectSingleNode("/PLS/companyInfo/project");
                PLCConnInfo.AppendFormat("项目名称：{0}\n",node.InnerText);
                node = xDocument.SelectSingleNode("/PLS/companyInfo/province");
                PLCConnInfo.AppendFormat("公司地址：{0}", node.InnerText);
                node = xDocument.SelectSingleNode("/PLS/companyInfo/city");
                PLCConnInfo.AppendFormat("{0}", node.InnerText);
                node = xDocument.SelectSingleNode("/PLS/companyInfo/distinction");
                PLCConnInfo.AppendFormat("{0}", node.InnerText);
                node = xDocument.SelectSingleNode("/PLS/companyInfo/address");
                PLCConnInfo.AppendFormat("{0}\n", node.InnerText);
                node = xDocument.SelectSingleNode("/PLS/companyInfo/contactPerson");
                PLCConnInfo.AppendFormat("公司联系人：{0}\n", node.InnerText);
                node = xDocument.SelectSingleNode("/PLS/companyInfo/tel");
                PLCConnInfo.AppendFormat("联系电话：{0}\n", node.InnerText);
                node = xDocument.SelectSingleNode("/PLS/companyInfo/fax");
                PLCConnInfo.AppendFormat("联系传真：{0}\n", node.InnerText);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                return null;
            }
            return PLCConnInfo.ToString();
        }

        public static BindingCollection<RemoteAndLocalUserInfo> GetPLCConnInfoUser(XmlDocument xDocument, int remoteOrlocal)
        {
            try
            {
                var users = new BindingCollection<RemoteAndLocalUserInfo>();
                XmlNodeList userList = xDocument.SelectNodes("/PLS/user");
                int parmNum = 0;
                if (userList != null)
                {
                    foreach (XmlNode userNodes in userList)
                    {
                        string[] tempUser = new string[4];
                        parmNum = 0;
                        foreach (XmlNode userNode in userNodes)
                        {
                            tempUser[parmNum++] = userNode.InnerText;
                        }
                        if (tempUser[0] == remoteOrlocal.ToString())
                            users.Add(new RemoteAndLocalUserInfo() {
                                    type = tempUser[0],
                                    userName = tempUser[1],
                                    tel = tempUser[2],
                                    email = tempUser[3]});
                        tempUser = null;
                    }
                }
                return users;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return null;
            }
        }

        public static BindingCollection<AbnormalInfo> GetAbnormalInfo(XmlDocument xDocument)
        {
            try
            {
                var abnormalInfo = new BindingCollection<AbnormalInfo>();
                XmlNodeList faults = xDocument.SelectNodes("/PLS/faultInformation");
                int parmNum = 0;
                if (faults != null)
                {
                    foreach (XmlNode faultNodes in faults)
                    {
                        string[] temp = new string[2];
                        parmNum = 0;
                        foreach (XmlNode faultNode in faultNodes)
                        {
                            temp[parmNum++] = faultNode.InnerText;
                        }
                            abnormalInfo.Add(new AbnormalInfo()
                            {
                                faultReason = temp[0],
                                faultTime = temp[1]});
                        temp = null;
                    }
                }
                return abnormalInfo;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return null;
            }
        
        }

        public static XmlWriter InitXml(NetworkStream netstream)
        {
            XmlWriterSettings setting = new XmlWriterSettings();
            setting.Indent = true;
            setting.IndentChars = "\t ";
            setting.Encoding = new UTF8Encoding(false);
            XmlWriter xmlWriter = XmlWriter.Create(netstream, setting);
            xmlWriter.WriteStartDocument();
            xmlWriter.WriteStartElement("PLS");

            return xmlWriter;
        }

        public static void AppendElement(XmlWriter xmlWriter, string tagName, string value)
        {
            xmlWriter.WriteStartElement(tagName);
            xmlWriter.WriteValue(value);
            xmlWriter.WriteEndElement();
        }

        public static void EndXml(XmlWriter xmlWriter)
        {
            xmlWriter.WriteEndElement();
            xmlWriter.WriteEndDocument();

            xmlWriter.Flush();
            xmlWriter.Close();
        }

        public static string GetUserID(XmlDocument xDocument)
        {
            XmlNode node;

            try
            {
                node = xDocument.SelectSingleNode("/PLS/userID");
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                return null;
            }
            if (node == null)
                return null;
            return node.InnerText;
        }

        public static string GetUserPwd(XmlDocument xDocument)
        {
            XmlNode node;

            try
            {
                node = xDocument.SelectSingleNode("/PLS/pwd");
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                return null;
            }
            if (node == null)
                return null;
            return node.InnerText;
        }

        public static string GetPLCID(XmlDocument xDocument)
        {
            XmlNode node;

            try
            {
                node = xDocument.SelectSingleNode("/PLS/PLCID");
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                return null;
            }
            if (node == null)
                return null;
            return node.InnerText;
        }

        public static string GetPLS(XmlDocument xDocument)
        {
            XmlNode node;

            try
            {
                node = xDocument.SelectSingleNode("/PLS");
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                return null;
            }
            if (node == null)
                return null;
            return node.InnerXml;
        }

        public static int CheckLoginResult(XmlDocument xDocument)
        {
            if (GetPLSVer(xDocument) == 1)
            {
                if (GetPLSType(xDocument) == 32)
                    return (GetPLSRetrunCode(xDocument));
                return -1;
            }
            else
                return -1;
        }
    
        public static string GetRemoteIp(XmlDocument xDocument)
        {
            XmlNode node;

            try
            {
                node = xDocument.SelectSingleNode("/PLS/remoteIp");
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                return null;
            }
            if (node == null)
                return null;
            return node.InnerXml;
        }

        public static string GetRemotePort(XmlDocument xDocument)
        {
            XmlNode node;

            try
            {
                node = xDocument.SelectSingleNode("/PLS/remotePort");
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                return null;
            }
            if (node == null)
                return null;
            return node.InnerXml;
        }
        
        public static string GetRemoteUserName(XmlDocument xDocument)
        {
            XmlNode node;

            try
            {
                node = xDocument.SelectSingleNode("/PLS/computername");
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                return null;
            }
            if (node == null)
                return null;
            return node.InnerXml;
        }

        public static string GetRemoteUserPwd(XmlDocument xDocument)
        {
            XmlNode node;

            try
            {
                node = xDocument.SelectSingleNode("/PLS/computerpasswd");
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                return null;
            }
            if (node == null)
                return null;
            return node.InnerXml;
        }

        public static string[] GetOldPersonalInfo(XmlDocument xDocument)
        {
            XmlNode node;
            string[] oldPersonalInfo = new string[3];
            try
            {
                node = xDocument.SelectSingleNode("/PLS/name");
                oldPersonalInfo[0] = node.InnerText;
                node = xDocument.SelectSingleNode("/PLS/tel");
                oldPersonalInfo[1] = node.InnerText;
                node = xDocument.SelectSingleNode("/PLS/email");
                oldPersonalInfo[2] = node.InnerText;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                return null;
            }
            if (node == null)
                return null;
            return oldPersonalInfo;
        }

        public static string[] GetNewPersonalInfo(XmlDocument xDocument)
        {
            XmlNode node;
            string[] newPersonalInfo = new string[5];
            try
            {
                node = xDocument.SelectSingleNode("/PLS/name");
                newPersonalInfo[0] = node.InnerText;
                node = xDocument.SelectSingleNode("/PLS/tel");
                newPersonalInfo[1] = node.InnerText;
                node = xDocument.SelectSingleNode("/PLS/email");
                newPersonalInfo[2] = node.InnerText;
                node = xDocument.SelectSingleNode("/PLS/oldPwd");
                newPersonalInfo[3] = node.InnerText;
                node = xDocument.SelectSingleNode("/PLS/newPwd");
                newPersonalInfo[4] = node.InnerText;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                return null;
            }
            if (node == null)
                return null;
            return newPersonalInfo;
        }
    }
}

